package com.github.skystardust.lotteryex.config;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;

import java.io.File;
import java.io.IOException;

public abstract class ConfigBase {
    private File file;
    private FileConfiguration fileConfiguration;
    private Plugin plugin;

    public ConfigBase(String name, Plugin plugin) {
        this.file = new File(plugin.getDataFolder(), name);
        this.plugin = plugin;
        if (!file.exists()) {
            try {
                plugin.saveResource(file.getName(), false);
            } catch (Exception e) {
                try {
                    file.createNewFile();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        this.fileConfiguration = YamlConfiguration.loadConfiguration(file);
        loadConfig();
    }

    public void reloadConfig() {
        setFileConfiguration(YamlConfiguration.loadConfiguration(getFile()));
        this.loadConfig();
    }

    public abstract void loadConfig();

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public FileConfiguration getFileConfiguration() {
        return fileConfiguration;
    }

    public void setFileConfiguration(FileConfiguration fileConfiguration) {
        this.fileConfiguration = fileConfiguration;
    }

    public Plugin getPlugin() {
        return plugin;
    }

    public void setPlugin(Plugin plugin) {
        this.plugin = plugin;
    }
}
